<?php
class MenusController extends AppController {
	var $name = 'Menus';
	var $helpers = array('Html');
	var $components = array('Init', 'Param');
	var $uses = array('Content');
	
	function beforeFilter() {
		if (!$this->Session->read('admin')) {
			$this->redirect('/users/login');
		}
		else {
			$this->Init->startup($this);
			$this->Admin = $this->Init->init();
			
			$this->set('dashboard',0);
			$this->set('content',1);
			$this->set('products',0);
			$this->set('component',0);
			$this->set('system',0);
			
			$this->set('title','Menus Management');
			$this->set('sub_title', $this->Param->getAction($this->params));
  		}
	}
	
	function index(){
		$this->loadModel('PageKind');
		$this->Content->loadJoin();
		$menus = $this->Content->find('all', array(
										'conditions' => array(
											'Content.status' => 1
											),
										'order' 	 => 'Content.date desc'
										));
		$this->set('menus',$menus);
	}
	
	function edit($id = null){
		$this->loadModel('PageKind');
		$id = abs(intval($id));
		
		if ($id < 1) {
			$this->redirect('/menus');	
		}
		
		$this->set('id', $id);
		
		if (isset($this->data)) {
			$this->data['Content']['id'] = $id;
			$this->data['Content']['name'] = trim(strip_tags($this->data['Content']['name']));
			$this->data['Content']['title'] = $this->data['Content']['title'];
			$this->data['Content']['date'] = time();

			if ($this->Content->save($this->data['Content'])) {
				$this->Session->setFlash('abc');
				$this->redirect(array('action' => 'menus'));
			}
		}
		
		$this->data = $this->Content->find('first',	array(
			'conditions' => array('Content.id' => $id)
		));
		
		$pagekind = $this->PageKind->find('list', array(
			'conditions' => array('PageKind.status' => 1),
			'fields' => array('PageKind.id', 'PageKind.name')
		));
		$this->set('pagekind',$pagekind);
	}
	
	function add(){
		$this->loadModel('PageKind');
		
		if (isset($this->data)) {
			$this->data['Content']['name'] = trim(strip_tags($this->data['Content']['name']));
			$this->data['Content']['title'] = trim(strip_tags($this->data['Content']['title']));
			$this->data['Content']['date'] = time();

			if ($this->Content->save($this->data['Content'])) {
				$this->Session->setFlash('abc');
				$this->redirect('/menus');
			}
		}
		
		$pagekind = $this->PageKind->find('list', array(
			'conditions' => array('PageKind.status' => 1),
			'fields' => array('PageKind.id', 'PageKind.name')
		));
		$this->set('pagekind',$pagekind);
	}
	
	function del($id = null) {
		 $this->Content->id = $id;
		 $this->Content->delete();
		 $this->Session->setFlash('aaa');
		 $this->redirect('/menus');
	}
	
}
?>